package com.cfec.educationplatform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cfec.educationplatform.util.ResultData;
import com.cfec.educationplatform.domain.User;
import com.cfec.educationplatform.mapper.UserMapper;
import com.cfec.educationplatform.service.UserService;
import com.cfec.educationplatform.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * @author wwy
 * @description 针对表【user】的数据库操作Service实现
 * @createDate 2025-03-03 16:00:47
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    TokenUtil tokenUtil;

    @Override
    public List<User> select1() {
        return userMapper.selectList(null);
    }

    @Override
    public ResultData select(User user) {
        HashMap<Object, Object> resultMap = new HashMap<>();
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.eq("user_account", user.getUserAccount())
                .eq("user_password", user.getUserPassword());
        User user1 = userMapper.selectOne(queryWrapper);
        if (user1 != null) {
            // 登录成功
            String token = new TokenUtil().createToken(user1);
            resultMap.put("token", token);
            resultMap.put("userId", user1.getUserId());
            return new ResultData(200, "登录成功", resultMap);
        } else {
            // 登录失败
            return new ResultData(501, "登陆失败", null);

        }

    }
}




